package com.example.word_book.service;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.word_book.R;
import com.example.word_book.utils.SPSave;
import com.example.word_book.utils.SqlHelp;

import java.util.Date;
import java.util.Map;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        init();
    }

    private void init() {
        Button login;

        CheckBox checkBox;

        TextView tv_register = findViewById(R.id.tv_register);
        login = findViewById(R.id.btn_login);

        EditText account, password;
        account = findViewById(R.id.edt_account);

        password = findViewById(R.id.edt_password);

        checkBox = findViewById(R.id.checkBox);

        Map<String, String> userInfo = SPSave.getUserInfo(this);
        if (SPSave.saveDateInfo(this, new Date())) {
//            SPSave.saveUserInfo(this, "", "", false);
            if (userInfo.get("isCheck").equals("isCheck")) {
                Intent i = getIntent();
                if (i.getStringExtra("flag") != null && i.getStringExtra("flag").equals("a")) {
                    account.setText(userInfo.get("account"));
                    password.setText(userInfo.get("password"));
                    checkBox.setChecked(true);
                } else {
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        } else {
            account.setText(userInfo.get("account"));
            SPSave.saveUserInfo(this, "", "", false);
        }

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s_account = account.getText().toString().trim();
                String s_password = password.getText().toString().trim();
                if (new SqlHelp(LoginActivity.this).selectUserAndPass(s_account, s_password)) {

                    if (SPSave.saveUserInfo(LoginActivity.this, s_account, s_password, checkBox.isChecked())) {
                        //存登录成功的账号信息
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(LoginActivity.this, "存储失败", Toast.LENGTH_SHORT).show();
                    }
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}